//
//  LineView.m
//  ViewDemo
//
//  Created by Angelo on 16/6/1.
//  Copyright © 2016年 Angelo. All rights reserved.
//

#import "LineView.h"

//颜色
#define COLORWITH_RGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1]

#define height_scale (self.frame.size.height / 95)

#define width_scale (self.frame.size.width / 440)

#define spacing 15


@interface LineView ()

@property(nonatomic,strong)UILabel *leftLable;

@property(nonatomic,strong)UILabel *rightLable;

@property(nonatomic,strong)UIView *leftView;

@property(nonatomic,strong)UIView *rightView;

@property(nonatomic,strong)UILabel *bottomLable;



@end



@implementation LineView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initSubviews];
        _upStr = @"0";
        _downStr = @"0";
    }
    return self;
}




- (void)initSubviews{
    self.backgroundColor = [UIColor cyanColor];
    UIButton *btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLeft.frame = CGRectMake(15 * width_scale, 15 * height_scale, 50 * width_scale, 25 * height_scale);
    btnLeft.layer.cornerRadius = 5;
    btnLeft.layer.borderWidth = 1;
    btnLeft.layer.borderColor = [[UIColor redColor]CGColor];
    [btnLeft setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [btnLeft setTitle:@"猜涨" forState:UIControlStateNormal];
    [btnLeft addTarget:self action:@selector(upBtnClickAction:)forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnLeft];
    
    _leftLable = [[UILabel alloc] initWithFrame:CGRectMake(70 * width_scale, 15 * height_scale, 50 * width_scale, 25 * height_scale)];
    [self addSubview:_leftLable];
    _leftLable.textAlignment = NSTextAlignmentCenter;
    _leftLable.textColor = [UIColor redColor];
    
    _rightLable = [[UILabel alloc] initWithFrame:CGRectMake(320 * width_scale, 15 * height_scale, 50 * width_scale, 25 * height_scale)];
    [self addSubview:_rightLable];
    _rightLable.textAlignment = NSTextAlignmentCenter;
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(375 * width_scale, 15 *height_scale, 50 * width_scale, 25 * height_scale);
    
    [rightBtn setTitle:@"猜跌" forState:UIControlStateNormal];
    
    [rightBtn setTitleColor:COLORWITH_RGB(0, 165, 20) forState:UIControlStateNormal];
    
    rightBtn .layer.borderWidth = 1;
    rightBtn.layer.cornerRadius = 5;
    rightBtn.layer.borderColor = [COLORWITH_RGB(0, 165, 20) CGColor];
    [rightBtn setTitleColor:COLORWITH_RGB(0, 165, 20) forState:UIControlStateNormal];
    
    [rightBtn addTarget:self action:@selector(downBtnClickAction:)forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    
    _leftView = [[UIView alloc] initWithFrame:CGRectMake(15 * width_scale, 55 * height_scale, 0, 5 * height_scale)];
    _leftView.backgroundColor = [UIColor redColor];
    [self addSubview:_leftView];
    _rightView = [[UIView alloc] initWithFrame:CGRectMake(15 * width_scale, 55 * height_scale, 0, 5 * height_scale)];
    _rightView.backgroundColor = COLORWITH_RGB(0, 165, 20);
    [self addSubview:_rightView];
    _bottomLable = [[UILabel alloc] initWithFrame:CGRectMake(15 * width_scale, 75 * height_scale, 410 * width_scale, 15 * height_scale)];
    _bottomLable.textColor = [UIColor grayColor];
    [self addSubview:_bottomLable];
}

- (void)downBtnClickAction:(UIButton*)sender{
    
    _downStr = [NSString stringWithFormat:@"%ld",[_downStr integerValue] + 1];
    if (self.downBlock) {
        self.downBlock();
    }
    [self changeViewFram];
    
}


- (void)upBtnClickAction:(UIButton*)sender{
    _upStr = [NSString stringWithFormat:@"%ld",[_upStr integerValue] + 1];
    
    if (self.downBlock) {
        
        self.downBlock();
    }
    [self changeViewFram];
}

- (void)setUpStr:(NSString *)upStr{
    _upStr = upStr;
    _leftLable.text = upStr;
    [self changeViewFram];
}

- (void)setDownStr:(NSString *)downStr{
    _downStr = downStr;
    _rightLable.text = downStr;
    [self changeViewFram];
}

- (void)setBottomLabelStr:(NSString *)bottomLabelStr{
    _bottomLabelStr = bottomLabelStr;
    _bottomLable.text = bottomLabelStr;
}

- (void)setBtnUpBlock:(btnClickBlock)block{
    self.upBlock = block;
}

- (void)setBtnDownBlock:(btnClickBlock)block{
    self.downBlock = block;
}

//修改线的fram和label的
- (void)changeViewFram{
    
    _leftLable.text = _upStr;
    _rightLable.text = _downStr;
    NSInteger leftLab = [_upStr integerValue];
    NSInteger rightLab = [_downStr integerValue];
    CGFloat width = 410 * width_scale;
    
    CGRect leftRect = _leftView.frame;
    CGRect rightRect = _rightView.frame;
    
    leftRect.size.width = leftLab * (width / (leftLab + rightLab));
    _leftView.frame = leftRect;
    
    rightRect.origin.x = leftRect.origin.x + leftRect.size.width;
    rightRect.size.width = rightLab * (width / (leftLab + rightLab));
    _rightView.frame = rightRect;
    
    
    
    
    
    
    
    
}


@end
